package ejemplo;

import java.io.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.util.regex.Pattern;

public class Confirm extends HttpServlet {

  private AccesoDatos ad;
  private ResultSet rs;
  private MyTableModel mtb;
  private String mensaje;

  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {} 

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {} 

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
    RequestDispatcher rd;
    ServletContext context = getServletContext();
    InputStream header = context.getResourceAsStream("/header.xhtml");
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    String table=request.getParameter("table");
    try {
      if (header != null) {
        InputStreamReader isr = new InputStreamReader(header);
        BufferedReader reader = new BufferedReader(isr);
        String line ="";
        if(table.equals("libros")){
           while ((line = reader.readLine()) != null){
               if(Pattern.matches(".*OPER.*", line))
                   out.print(line.replaceFirst("OPER","Confirmar borrar libro"));
               else out.print(line);
           }
        } //endif table == libros
        else if(table.equals("temas")){
            while ((line = reader.readLine()) != null){
                if(Pattern.matches(".*OPER.*", line))
                    out.print(line.replaceFirst("OPER","Confirmar borrar tema"));
                else out.print(line);
            }
        } //endif table == temas
      } //endif if (header != null)
      // consulta y cuerpo
      if(table.equals("libros")){
         String isbn=request.getParameter("isbn");
         ad = new AccesoDatos("gencod");
         rs = ad.getLibro(isbn);
         mtb = new MyTableModel(rs);
         
         //leer plantilla
         InputStream abmc_libro = context.getResourceAsStream("/abmc-libro.xhtml");
         if (abmc_libro != null) {
             InputStreamReader isr = new InputStreamReader(abmc_libro);
             BufferedReader reader = new BufferedReader(isr);
             String line ="";
             while ((line = reader.readLine()) != null){
               if(Pattern.matches(".*ACTION.*", line))
                   out.println(line.replaceFirst("ACTION", "\"./delete\""));
               else if(Pattern.matches(".*VALUE1.*", line)){
                   line = line.replace(line,
                   "<input type=Hidden name=isbn value=\""
                   + mtb.getValueAt(0, 1) + "\"/>&nbsp;" + mtb.getValueAt(0, 1));
                   out.println(line);
               }
               else if(Pattern.matches(".*VALUE2.*", line)){
                   line = line.replace(line,"&nbsp;" + mtb.getValueAt(0, 2));
                   out.println(line);
               }
               else if(Pattern.matches(".*VALUE3.*", line)){
                   line = line.replace(line,"&nbsp;" + mtb.getValueAt(0, 3));
                   out.println(line);
               }
               else if(Pattern.matches(".*VALUE4.*", line)){
                   line = line.replace(line,"&nbsp;" + mtb.getValueAt(0, 4));
                   out.println(line);
               }
               else if(Pattern.matches(".*SELECT.*", line)){
                   out.println(line.replaceFirst("SELECT", ""));
                   out.println("<option>" + mtb.getValueAt(0, 5) + "</option>");
               }
               else if(Pattern.matches(".*BUTTON1.*", line))
                   out.println(line.replaceFirst("BUTTON1", "\"Borrar\""));
               else if(Pattern.matches(".*BACK.*", line))
                   out.println(line.replaceFirst("BACK", "\"./select\""));
               else out.println(line);
             }
         } //endif if (abmc_libro != null)
         out.println("<center><h2>");
         out.println("<font color=\"#FFFFFF\">");
         out.println("Desea borrar este libro?");
         out.println("</font></h2></center>");     
      } //endif(table.equals("libros"))
      else if(table.equals("temas")){
         String id=request.getParameter("id");
         ad = new AccesoDatos("gencod");
         rs = ad.getTema(id);
         mtb = new MyTableModel(rs);
         //leer plantilla
         InputStream abmc_tema = context.getResourceAsStream("/abmc-tema.xhtml");
         if (abmc_tema != null) {
             InputStreamReader isr = new InputStreamReader(abmc_tema);
             BufferedReader reader = new BufferedReader(isr);
             String line ="";
             while ((line = reader.readLine()) != null){
               if(Pattern.matches(".*ACTION.*", line)){
                   out.println(line.replaceFirst("ACTION", "\"./delete\""));
                   out.println("<input type=Hidden name=id value=\"" + mtb.getValueAt(0, 0) + "\"/>");
               }
               else if(Pattern.matches(".*VALUE1.*", line)){
                   line = line.replace(line,"&nbsp;" + mtb.getValueAt(0, 1));
                   out.println(line);
               }               
               else if(Pattern.matches(".*BUTTON1.*", line))
                   out.println(line.replaceFirst("BUTTON1", "\"Borrar\""));
               else if(Pattern.matches(".*BACK.*", line))
                   out.println(line.replaceFirst("BACK", "\"./select\""));
               else out.println(line);
             }
         } //endif if (abmc_tema != null)
         out.println("<center><h2>");
         out.println("<font color=\"#FFFFFF\">");
         out.println("Desea borrar este tema?");
         out.println("</font></h2></center>");     
      } //endif(table.equals("temas"))
    } catch(ClassNotFoundException cnfe){
      rd=request.getRequestDispatcher("/index");
      mensaje="Error fatal. Excepcion de clase no encontrada: " + cnfe.getMessage();
      request.setAttribute("mensaje",mensaje);
      rd.forward(request,response);
    } catch(SQLException sqle){
      rd=request.getRequestDispatcher("/index");
      mensaje="Error fatal. Excepcion de SQL: " + sqle.getErrorCode();
      request.setAttribute("mensaje",mensaje);
      rd.forward(request,response);
    }
    finally {   out.close();   }
 }

  @Override
  public String getServletInfo(){ return "Short description"; }
}
